﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.SaleSystem.Catalog;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using wnxd.javascript;

    public class Common_SKUSelector : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            DataTable skus = ProductBrowser.GetSkus(this.ProductId);
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("<input type=\"hidden\" id=\"hiddenSkuId\" value=\"{0}_0\"  />", this.ProductId).AppendLine();
            if ((skus != null) && (skus.Rows.Count > 0))
            {
                IList<string> list = new List<string>();
                builder.AppendFormat("<input type=\"hidden\" id=\"hiddenProductId\" value=\"{0}\" />", this.ProductId).AppendLine();
                builder.AppendLine("<div class=\"specification\">");
                dynamic wjson = new json(new object[0]);
                int i = 0;
                foreach (DataRow row in skus.Rows)
                {
                    if (row["ProductId"] != DBNull.Value)
                    {
                        int index = 0;
                        for (; index < wjson.length; index++)
                        {
                            string skuid = wjson[index].skuid;
                            if (skuid == row["SkuId"].ToString())
                            {
                                break;
                            }
                        }
                        if (index == wjson.length)
                        {
                            index = wjson.push(new { skuid = row["SkuId"], index = new object[] { }, info = new { pid = row["ProductId"] } });
                        }
                        wjson[index][row["AttributeId"].ToString()] = new json(row["ValueId"]);
                    }
                }
                foreach (DataRow row in skus.Rows)
                {
                    if (!list.Contains((string) row["AttributeName"]))
                    {
                        list.Add((string) row["AttributeName"]);
                        builder.AppendFormat("<div class=\"title text-muted\">{0}：</div><input type=\"hidden\" name=\"skuCountname\" AttributeName=\"{0}\" id=\"skuContent_{1}\" />", row["AttributeName"], row["AttributeId"]);
                        builder.AppendFormat("<div class=\"list clearfix wnxdselect\" id=\"skuRow_{0}\">", row["AttributeId"]);
                        IList<string> list2 = new List<string>();
                        foreach (DataRow row2 in skus.Rows)
                        {
                            if ((string.Compare((string) row["AttributeName"], (string) row2["AttributeName"]) == 0) && !list2.Contains((string) row2["ValueStr"]))
                            {
                                for (int m = 0; m < wjson.length; m++)
                                {
                                    if (wjson[m][row2["AttributeId"].ToString()].Equals(row2["ValueId"]))
                                    {
                                        wjson[m]["index"].push(i);
                                    }
                                }
                                i++;
                                string str = string.Concat(new object[] { "skuValueId_", row2["AttributeId"], "_", row2["ValueId"] });
                                list2.Add((string) row2["ValueStr"]);
                                builder.AppendFormat("<div class=\"SKUValueClass\" id=\"{0}\" AttributeId=\"{1}\" ValueId=\"{2}\">{3}</div>", new object[] { str, row2["AttributeId"], row2["ValueId"], row2["ValueStr"] });
                            }
                        }
                        builder.AppendLine("</div>");
                    }
                }
                builder.AppendLine("</div>");
                builder.Append("<script type=\"text/javascript\">");
                builder.AppendFormat("var selectInfo = {0};", wjson.ToString());
                for (int n = 0; n < wjson.length; n++)
                {
                    if (wjson[n]["info"]["pid"].ToString() == this.ProductId.ToString())
                    {
                        builder.AppendFormat("var nowIndex = {0};", wjson[n]["index"]);
                        builder.AppendFormat("var nowPid = {0};", this.ProductId);
                        break;
                    }
                }
                builder.Append("</script>");
            }
            writer.Write(builder.ToString());
        }

        public int ProductId { get; set; }
    }
}

